<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    width="30%"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="品牌名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="品牌名称"></el-input>
    </el-form-item>
    <el-form-item label="品牌排序" prop="sort">
      <el-input v-model="dataForm.sort" placeholder="品牌排序" type="number"></el-input>
    </el-form-item>
    <el-form-item label="商铺列表" prop="shopId">
      <el-select
        v-model="dataForm.shopId"
        filterable
        placeholder="请选择商铺">
        <el-option
          v-for="item in shopList"
          :key="item.id"
          :label="item.shopName"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {getShopsByType} from '@/api/shop/shops'
  export default {
    data () {
      return {
        visible: false,
        shopList: [],
        brandTypeList: [],
        dataForm: {
          id: 0,
          name: '',
          sort: 100,
          shopId: ''
        },
        dataRule: {
          name: [
            { required: true, message: '品牌名称不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '品牌排序不能为空', trigger: 'blur' }
          ],
          shopId: [
            { required: true, message: '商铺不能为空', trigger: 'change' }
          ]
        }
      }
    },
    methods: {
      getShopList() {
        const params = new FormData()
        params.append('shopId', this.$store.state.user.shopId)
        params.append('type',4)
        getShopsByType(params)
          .then((res) => {
            this.shopList = res.data.data
          })
      },
      init (id) {
        this.getShopList()
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/merchantservices/brand/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.data.name
                this.dataForm.sort = data.data.sort
                this.dataForm.shopId = data.data.shopId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/merchantservices/brand/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'sort': this.dataForm.sort,
                'shopId': this.dataForm.shopId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                })
                this.visible = false
                this.$emit('refreshDataList')
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
    }
  }
</script>
